package guice.boot.archive.bootstrap;

import java.lang.reflect.Method;


public class MainMethodRunner {

    private final String mainClassName;

    private final String[] args;

    public MainMethodRunner(String mainClass, String[] args) {
        this.mainClassName = mainClass;
        this.args = (args == null ? null : args.clone());
    }

    public Object invoke() throws Exception {
        /**
         * Utility class that is used  to call a main method. The class containing the main method is loaded using the thread context class loader.
         */
        Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(this.mainClassName);
        Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
        return mainMethod.invoke(null, new Object[]{this.args});
    }

}